package com.siwei.mes.service.laboratory.factory.material.cement;

import com.alibaba.fastjson.JSONObject;
import com.siwei.mes.common.util.JsonUtils;
import com.siwei.mes.common.util.NullUtils;
import com.siwei.mes.entity.experiment.CheckBatchConfig;
import com.siwei.mes.entity.experiment.Experiment;
import com.siwei.mes.entity.experiment.ExperimentDetail;
import com.siwei.mes.entity.experiment.cement.CementQdcd;
import com.siwei.mes.entity.laboratory.cement.DataSnQDDJ28D;
import com.siwei.mes.entity.laboratory.cement.DataSnQDDJ3D;
import com.siwei.mes.entity.system.TestProjectInfo;
import com.siwei.mes.entity.system.TestProjectSpecConfig;
import com.siwei.mes.enums.TestProjectEnum;
import com.siwei.mes.mapper.experiment.ExperimentDetailMapper;
import com.siwei.mes.mapper.laboratory.CementLaboratoryMapper;
import com.siwei.mes.service.laboratory.factory.LaboratoryAbstractService;
import com.siwei.mes.service.laboratory.factory.entity.LaboratoryDownloadVo;
import com.siwei.mes.service.laboratory.factory.material.MaterialLaboratoryFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 水泥-强度测定
 *
 * @Description: 水泥-强度测定
 * @Version: 1.0
 * @author: mlchen
 * @date: 2025/4/3 16:08
 */
@Service
public class CementQdcdFactory extends LaboratoryAbstractService implements MaterialLaboratoryFactory {
    private static final Logger log = LoggerFactory.getLogger("laboratory");

    @Resource
    private CementLaboratoryMapper cementLaboratoryMapper;
    @Resource
    private ExperimentDetailMapper experimentDetailMapper;


    private final TestProjectEnum testProjectEnum = TestProjectEnum.CEMENT_PARAM_QDCD;

    @Override
    public void processUpload(JSONObject objJson, String sampleNo, Experiment experiment, List<CheckBatchConfig> batchConfigList, List<TestProjectInfo> testProjectInfoList, List<TestProjectSpecConfig> testProjectSpecConfigList, ExperimentDetail detail, String samId) {
        log.info("开始上传到协会数据库：【水泥-{}】", testProjectEnum.getName());
        log.info("暂不需要上传！");
    }

    @Override
    public LaboratoryDownloadVo processDownload(String sampleNo, Experiment experiment, ExperimentDetail detail, Map<String, String> parameterMap, String samId, List<TestProjectInfo> testProjectInfoList) {
        log.info("开始下载协会数据到品控：【水泥-{}】", testProjectEnum.getName());
        JSONObject objJson = detail.getObjJson();
        //将json转成对象
        CementQdcd cementQdcd = objJson.toJavaObject(CementQdcd.class);
        if (cementQdcd == null) {
            log.info("水泥-【{}】数据为空，跳过上传！objJson：{}", testProjectEnum.getName(), objJson);
            return null;
        }
        //根据样品编码查询水泥协会抗压 抗折 3d
        DataSnQDDJ3D data = cementLaboratoryMapper.findDATA_JGCL_SN_QDDJ3DBySampleNo(sampleNo);
        //根据样品编码查询水泥协会抗压 抗折 28d
        DataSnQDDJ28D dataT = cementLaboratoryMapper.findDATA_JGCL_SN_QDDJ28DBySampleNo(sampleNo);


        //3d
        CementQdcd.Kyqdsy kyqdsy3d = cementQdcd.getKyqdsy3d();
        if (data != null && kyqdsy3d != null) {
            if (data.getConclusion2() != null) {
                kyqdsy3d.setDxjl(data.getConclusion2());
            }
            if (data.getS17() != null) {
                kyqdsy3d.setPjz(data.getS17());
            }
            List<CementQdcd.KyqdsyInfo> kyqdsyInfo = kyqdsy3d.getKyqdsyInfo();
            int con = 1;
            for (CementQdcd.KyqdsyInfo ky : kyqdsyInfo) {
                if (con == 1) {
                    if (data.getS5() != null) {
                        ky.setHz(data.getS5());
                    }
                    if (data.getS6() != null) {
                        ky.setKyqd(data.getS6());
                    }
                } else if (con == 2) {
                    if (data.getS7() != null) {
                        ky.setHz(data.getS7());
                    }
                    if (data.getS8() != null) {
                        ky.setKyqd(data.getS8());
                    }
                } else if (con == 3) {
                    if (data.getS9() != null) {
                        ky.setHz(data.getS9());
                    }
                    if (data.getS10() != null) {
                        ky.setKyqd(data.getS10());
                    }
                } else if (con == 4) {
                    if (data.getS11() != null) {
                        ky.setHz(data.getS11());
                    }
                    if (data.getS12() != null) {
                        ky.setKyqd(data.getS12());
                    }
                } else if (con == 5) {
                    if (data.getS13() != null) {
                        ky.setHz(data.getS13());
                    }
                    if (data.getS14() != null) {
                        ky.setKyqd(data.getS14());
                    }
                } else if (con == 6) {
                    if (data.getS15() != null) {
                        ky.setHz(data.getS15());
                    }
                    if (data.getS16() != null) {
                        ky.setKyqd(data.getS16());
                    }
                }
                con++;
            }
        }

        CementQdcd.Kzqdsy kzqdsy3d = cementQdcd.getKzqdsy3d();
        if (data != null && kzqdsy3d != null) {
            if (data.getConclusion() != null) {
                kzqdsy3d.setDxjl(data.getConclusion());
            }
            if (data.getS4() != null) {
                kzqdsy3d.setPjz(data.getS4());
            }
            if (data.getDetectonDate() != null) {
                Date detectonDate = data.getDetectonDate();
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                String strDate = sdf.format(detectonDate);
                cementQdcd.setJcrq3d(strDate);
            }

            List<CementQdcd.KzqdsyInfo> kzqdsyInfo = kzqdsy3d.getKzqdsyInfo();

            int con = 1;
            for (CementQdcd.KzqdsyInfo kz : kzqdsyInfo) {
                if (con == 1) {
                    if (data.getS1() != null) {
                        kz.setKzqd(data.getS1());
                    }
                } else if (con == 2) {
                    if (data.getS2() != null) {
                        kz.setKzqd(data.getS2());
                    }
                } else if (con == 3) {
                    if (data.getS3() != null) {
                        kz.setKzqd(data.getS3());
                    }
                }
                con++;
            }

        }


        //28d
        CementQdcd.Kyqdsy kyqdsy28d = cementQdcd.getKyqdsy28d();
        if (dataT != null && kyqdsy28d != null) {
            if (dataT.getConclusion2() != null) {
                kyqdsy28d.setDxjl(dataT.getConclusion2());
            }
            if (dataT.getS17() != null) {
                kyqdsy28d.setPjz(dataT.getS17());
            }
            List<CementQdcd.KyqdsyInfo> kyqdsyInfo = kyqdsy28d.getKyqdsyInfo();
            int con = 1;
            for (CementQdcd.KyqdsyInfo ky : kyqdsyInfo) {
                if (con == 1) {
                    if (dataT.getS5() != null) {
                        ky.setHz(dataT.getS5());
                    }
                    if (dataT.getS6() != null) {
                        ky.setKyqd(dataT.getS6());
                    }
                } else if (con == 2) {
                    if (dataT.getS7() != null) {
                        ky.setHz(dataT.getS7());
                    }
                    if (dataT.getS8() != null) {
                        ky.setKyqd(dataT.getS8());
                    }
                } else if (con == 3) {
                    if (dataT.getS9() != null) {
                        ky.setHz(dataT.getS9());
                    }
                    if (dataT.getS10() != null) {
                        ky.setKyqd(dataT.getS10());
                    }
                } else if (con == 4) {
                    if (dataT.getS11() != null) {
                        ky.setHz(dataT.getS11());
                    }
                    if (dataT.getS12() != null) {
                        ky.setKyqd(dataT.getS12());
                    }
                } else if (con == 5) {
                    if (dataT.getS13() != null) {
                        ky.setHz(dataT.getS13());
                    }
                    if (dataT.getS14() != null) {
                        ky.setKyqd(dataT.getS14());
                    }
                } else if (con == 6) {
                    if (dataT.getS15() != null) {
                        ky.setHz(dataT.getS15());
                    }
                    if (dataT.getS16() != null) {
                        ky.setKyqd(dataT.getS16());
                    }
                }
                con++;
            }
        }

        CementQdcd.Kzqdsy kzqdsy28d = cementQdcd.getKzqdsy28d();
        if (dataT != null && kzqdsy28d != null) {
            if (dataT.getConclusion() != null) {
                kzqdsy28d.setDxjl(dataT.getConclusion());
            }
            if (dataT.getS4() != null) {
                kzqdsy28d.setPjz(dataT.getS4());
            }
            if (dataT.getDetectonDate() != null) {
                Date detectonDate = dataT.getDetectonDate();
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                String strDate = sdf.format(detectonDate);
                cementQdcd.setJcrq28d(strDate);
            }

            List<CementQdcd.KzqdsyInfo> kzqdsyInfo = kzqdsy28d.getKzqdsyInfo();

            int con = 1;
            for (CementQdcd.KzqdsyInfo kz : kzqdsyInfo) {
                if (con == 1) {
                    if (dataT.getS1() != null) {
                        kz.setKzqd(dataT.getS1());
                    }
                } else if (con == 2) {
                    if (dataT.getS2() != null) {
                        kz.setKzqd(dataT.getS2());
                    }
                } else if (con == 3) {
                    if (dataT.getS3() != null) {
                        kz.setKzqd(dataT.getS3());
                    }
                }
                con++;
            }
        }

        //对象转json
        JSONObject jsonObject = JsonUtils.beanToObject(cementQdcd);

        //重新赋值
        detail.setObjJson(jsonObject);

        if(data != null){
            detail.setEquipment(NullUtils.ifNull(data.getEquipment()));
            detail.setTemperature(NullUtils.ifNull(data.getTemperature()));
            detail.setHumidness(NullUtils.ifNull(data.getHumidity()));
            detail.setExperimentGist(NullUtils.ifNull(data.getDetectonRuleCode()));
            detail.setJudgeGist(NullUtils.ifNull(data.getAccessRuleCode()));
        }



        if (parameterMap != null && parameterMap.get(detail.getTestProjectCode()) != null) {
            detail.setStatus(parameterMap.get(detail.getTestProjectCode()));
        }

        //修改品控试验数据
        experimentDetailMapper.updateByPrimaryKeySelective(detail);

        return new LaboratoryDownloadVo().setEquipment(detail.getEquipment())
                .setJudgeGist(detail.getJudgeGist())
                .setExperimentGist(detail.getExperimentGist())
                .setTemperature(detail.getTemperature())
                .setHumidness(detail.getHumidness());
    }
}
